package cn.lan.generator.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.lan.generator.bean.EntityInfo;
import cn.lan.generator.bean.Module;
import cn.lan.generator.bean.ProjInfo;
import cn.lan.generator.service.CreateFile;
import cn.lan.generator.utils.ConfigUtil;
import cn.lan.generator.utils.Constant;
import cn.lan.generator.utils.FileUtil;
import cn.lan.generator.utils.StringUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lanping
 * @version 1.0
 * @date 2019-07-06
 **/
public class CreateControllerImpl implements CreateFile {

    /**
     * 创建Controller文件
     *
     * @param entityInfo 实体信息
     * @param projInfo   项目信息
     * @throws Exception
     */
    @Override
    public void writeFile(EntityInfo entityInfo, ProjInfo projInfo) throws Exception {

        String tableComment = entityInfo.getTableComment();
        String lowerPrimaryKey = StringUtil.toLowerCaseFirstOne(entityInfo.getPrimaryKey());
        String upperPrimaryKey = StringUtil.toUpperCaseFirstOne(entityInfo.getPrimaryKey());
        String primaryKeyType = entityInfo.getPrimaryKeyType();

        String upperEntityName = StringUtil.toUpperCaseFirstOne(entityInfo.getEntityName());
        String lowerEntityName = StringUtil.toLowerCaseFirstOne(entityInfo.getEntityName());

        String basePackage = projInfo.getPackageName();
        String packageName = ConfigUtil.getControllerPackage(basePackage);
        String entityPackName = ConfigUtil.getEntityPackage(projInfo);
        String listDtoPackage = ConfigUtil.getListDtoPackage(projInfo);
        String voPackName = ConfigUtil.getVoPackage(projInfo);
        String beanCommonPackName = ConfigUtil.getCommonBeanPackage(basePackage);
        String utilsPackage = ConfigUtil.getCommonUtilsPackage(basePackage);
        String servicePackage = ConfigUtil.getServicePackage(projInfo);
        String accessBaseUrl = projInfo.getAccessBaseUrl();
        String annotationPackage = ConfigUtil.getAnnotationPackage(basePackage);
        String enumsPackage = ConfigUtil.getEnumsPackage(basePackage);

        String srcFilePath = FileUtil.getResourceRootDir(projInfo.isDebugModel())+Constant.TEMPLATE_CONTROLLER_PATH;

        String suffixPath = packageName.replaceAll("\\.", "\\\\") + "\\" + upperEntityName + "Controller" + Constant.FILE_SUFFIX_JAVA;
        String destFilePath = Constant.getFilePath(projInfo, Module.getModule(projInfo).getWebServer(), Constant.PathType.basePath, suffixPath);

        Map<String,String> map = new HashMap<String,String>(){
            {
                put("filePackageName",packageName);
                put("entityPackage",entityPackName);
                put("commonBeanPackage",beanCommonPackName);
                put("listDtoPackage",listDtoPackage);
                put("voPackage",voPackName);
                put("upperEntityName",upperEntityName);
                put("tableComment",tableComment);
                put("projectAuthor",projInfo.getAuthor());
                put("nowDate", DateUtil.today());
                put("lowerEntityName",lowerEntityName);
                put("upperPrimaryKey",upperPrimaryKey);
                put("lowerPrimaryKey",lowerPrimaryKey);
                put("utilsPackage",utilsPackage);
                put("servicePackage",servicePackage);
                put("accessBaseUrl",accessBaseUrl);
                put("annotationPackage",annotationPackage);
                put("enumsPackage",enumsPackage);
                put("primaryType",primaryKeyType);
                put("primaryKeyComment",entityInfo.getPrimaryKeyComment());
            }
        };
        FileUtil.copyFile(projInfo,srcFilePath, destFilePath, map);
    }
}
